//
//  WCUpdatePicCell.m
//  WC
//
//  Created by wsj2015 on 16/1/25.
//  Copyright © 2016年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCUpdatePicCell.h"
#import "WCOrderPicCell.h"
#import "WSJAnimationTool.h"
#import "Masonry.h"
#import "WCUpdateImageModel.h"
#import "WCImageManager.h"
#import "WCImageView +setImage.h"
#import "WSJImage.h"





@implementation WCUpdatePicCell 
{
   
    UIView *_bgView;
    BOOL _isShaking;
    
    UIImageView *_currentImage;
    NSMutableArray  *_temArrary;
    BOOL _isAbleDelect;
    

}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self =[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        
        
//        _imageContains = [NSMutableArray array];
        UIView *bgView=[[UIView alloc] initWithFrame:CGRectMake(8, 0, kScreenWidth -16, 110)];
        bgView.layer.borderWidth =1;
        bgView.layer.borderColor = kBorderColor.CGColor;
        bgView.layer.cornerRadius =5;
        bgView.layer.masksToBounds = YES;
        _bgView = bgView;
        [self.contentView addSubview:bgView];
        
        [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left);
             make.right.equalTo(self.contentView.mas_right);
             make.top.equalTo(self.contentView.mas_top);
             make.bottom.equalTo(self.contentView.mas_bottom);
        }];
        
        [self createImageViews];
        
        
    }
    return self;
    

}
- (void)createImageViews
{
    
    CGFloat w =([UIScreen mainScreen].bounds.size.width-32)/3;
    CGFloat h =[UIScreen mainScreen].bounds.size.width /3.422;
    
    for (int i =0; i<3; i++) {
        GestureImageView *imageView =[[GestureImageView alloc] initWithFrame:CGRectMake(16+(w+8)*i, 8,w, h-16)];
        imageView.image  =[UIImage imageNamed:@"图片占位"];
        
       
        WCUpdateImageModel *model =[[WCUpdateImageModel alloc] init];
        model.imageId =i;
        
        
        model.feature =0;
        model.bigImage =[UIImage imageNamed:@"图片占位"];
        imageView.imageModel =model;
        imageView.tag = 2000+i;
        imageView.userInteractionEnabled = YES;
        imageView.feature = GestureImageViewFeatureFeaturePlaceHoler;
        imageView.tapG =[self createTapG];
        imageView.longG =[self createLongG];
        
        [imageView.longG removeTarget:self action:@selector(longAction:)];
        [_bgView addSubview:imageView];
        
        UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame =CGRectMake(w-25, 0, 25, 25);
        [btn addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
        //        btn.alpha = 0;
        btn.tag = 300;
        btn.alpha = 0;
        [btn setBackgroundImage:[UIImage imageNamed:@"dele_iocn"] forState:UIControlStateNormal];
        
        //        [btn setBackgroundColor:[UIColor redColor]];
        [imageView addSubview:btn];
        \
        CGFloat left_make =8+(w+8)*i;
        if (i ==0) {
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(_bgView.mas_left).offset(left_make);
                make.top.equalTo(_bgView.mas_top).offset(8),
                make.width.mas_equalTo(w);
//                make.height.mas_equalTo(h -16);
                make.bottom.equalTo(_bgView.mas_bottom).offset(-8);
                
            }];
            
            
        }else if(i ==1){
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(_bgView.mas_left).offset(left_make);
                make.top.equalTo(_bgView.mas_top).offset(8),
                make.width.mas_equalTo(w);
                make.bottom.equalTo(_bgView.mas_bottom).offset(-8);
            }];
            
        }else if(i ==2){
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(_bgView.mas_left).offset(left_make);
                make.top.equalTo(_bgView.mas_top).offset(8),
                make.width.mas_equalTo(w);
                make.bottom.equalTo(_bgView.mas_bottom).offset(-8);
            }];
            
        }
        
        
        
        
    }
    
    
    
    
}


- (UILongPressGestureRecognizer *)createLongG
{
    UILongPressGestureRecognizer *longPress =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)];
    // 设置长按手势需要几个手指同时按住 ，默认为1
    longPress.numberOfTouchesRequired = 1;
    
    // 设置长按手势最小的判定生效时间
    longPress.minimumPressDuration = 0.5;
    return longPress;
}
- (UITapGestureRecognizer *)createTapG
{
    return [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(picImage:)];
    
}
- (void)picImage:(UITapGestureRecognizer *)tapG
{
    
    if (_isShaking) {
        return;
    }
    UIImageView *imageView =(UIImageView *)tapG.view;
    
    _currentImage =imageView;
    
    UIActionSheet *picSheet =[[UIActionSheet alloc]initWithTitle:@"选择照片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册中获取",@"拍照", nil];
    
    [picSheet showInView:[UIApplication sharedApplication].keyWindow];
    
    
}
//选项表
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    
    switch (buttonIndex) {
        case 0:
            
            [self localPhoto];
            
            break;
        case 1:
            [self takePhoto];
            break;
            
        default:
            _currentImage =nil;
            break;
    }
}
// 照相操作
- (void)takePhoto
{
    UIImagePickerControllerSourceType type =UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable:type]) {
        UIImagePickerController *picker =[[UIImagePickerController alloc]init];
        picker.delegate =self;
        picker.allowsEditing =YES;
        picker.sourceType =type;
        picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
        if (self.delegateController) {
            [self.delegateController() presentViewController:picker animated:YES completion:nil];
        }
    }
    
}
//本地操作
- (void)localPhoto
{
    UIImagePickerControllerSourceType type =UIImagePickerControllerSourceTypePhotoLibrary;
    UIImagePickerController *picker =[[UIImagePickerController alloc]init];
    picker.sourceType =type;
    picker.delegate =self;
    [self.delegateController() presentViewController:picker animated:YES completion:nil];
    
    
    
}
- (void)dealloc{

  
}
#pragma -mark 图片 协议方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image =info[UIImagePickerControllerOriginalImage];
    WSJImage *wimage =[WSJImage new];
    wimage.bigImage = image;
    
    [_temArrary addObject:wimage];
    if (_currentImage) {
        GestureImageView *imageView =(GestureImageView *)[_bgView viewWithTag:_currentImage.tag];
        imageView.feature =GestureImageViewFeatureFeatureNew;
        imageView.imageModel.feature =1;
        imageView.imageModel.bigImage =image;
//        [imageView.tapG removeTarget:self action:@selector(picImage:)];
        [imageView.longG addTarget:self action:@selector(longAction:)];
        _currentImage.image = wimage.thumbImage;
        
        NSLog(@"_currentImage=====%i",_currentImage.tag);
        
    }
    
    _currentImage = nil;
    
    [self.delegateController() dismissViewControllerAnimated:YES completion:nil];
    
    
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    
    
    [self.delegateController() dismissViewControllerAnimated:YES completion:nil];
    
}



- (void)deleteAction:(UIButton *)btn
{
    btn.alpha =0;
    GestureImageView *imageView =(GestureImageView *)btn.superview;
    imageView.feature= GestureImageViewFeatureFeaturePlaceHoler;
    if (imageView.imageModel.feature ==2) {
        
         imageView.imageModel.isDelete = 1;
    }
    imageView.imageModel.feature = 0;
    
    
    [_temArrary removeObject:imageView.image];
    [self shakeAnimation:NO withView:imageView];
    [imageView setImage:[UIImage imageNamed:@"图片占位"]];
    
    [imageView.tapG addTarget:self action:@selector(picImage:)];
    [imageView.longG removeTarget:self action:@selector(longAction:)];
    
    
}

- (void)longAction:(UILongPressGestureRecognizer *)longG
{
    if (longG.state == UIGestureRecognizerStateEnded) {
        return;
    }
    UIImageView *tapView =(UIImageView *)longG.view;
    if (_isShaking ==NO) {
        [self shakeAnimation:YES withView:tapView];
    }else{
        [self shakeAnimation:NO withView:tapView];
    }
    
    
}

- (void)shakeAnimation:(BOOL)start withView:(UIView *)tapView
{
    [WSJAnimationTool shakeAnimationWithView:tapView];
    UIButton *btn =(UIButton *)[tapView viewWithTag:300];
    if (start) {
        _isShaking = YES;
        
        btn.alpha =1;
        [WSJAnimationTool animationMoveUp:btn duration:0.34f];
    }else{
        [tapView.layer removeAllAnimations];
        _isShaking =NO;
        btn.alpha =0;
        [WSJAnimationTool animationMoveDown:btn duration:0.34f];
        
    }
    
}


- (GestureImageView *)getPicImageView:(NSInteger)index{
    GestureImageView *imageView =[_bgView viewWithTag:2000 +index];
    return imageView;

}

- (void)setThreePics:(NSArray *)threePics{
    for (WCUpdateImageModel *model in threePics) {
          GestureImageView *imageView = [self getPicImageView:model.imageId];
        if (model.feature==2) {
            [imageView wsjSetBigImageWithUrl:model.imageName successBlock:^(UIImage *image) {
                model.bigImage  =image;
                imageView.imageModel =model;
                imageView.image = model.thumbImage;
                [imageView.longG addTarget:self action:@selector(longAction:)];
            }];
            
        }
       
       
    }

}
-(NSArray *)allPics{
    NSMutableArray *array =[NSMutableArray array];
    for (GestureImageView *sub in _bgView.subviews) {
        
        [array addObject:sub.imageModel];
    }
    return array;

}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)setEditAble:(BOOL)editAble{
    _editAble = editAble;
    self.userInteractionEnabled =editAble;
//    
//    for (GestureImageView *view in _bgView.subviews) {
//        //移除所有手势
//        for (UIGestureRecognizer *g in view.gestureRecognizers) {
//            [view removeGestureRecognizer:g];
//        }
//    }

}
@end
